/* <![CDATA[ */
jQuery(document).ready(function() {
	jQuery("a[class*=fancybox]").fancybox({
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000000',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'easingIn' : 'easeOutBack',
		'easingOut' : 'easeInBack',
		'speedIn' : '700',
		'centerOnScroll' : true
	});

	jQuery("a[class*='realestate_video_lightbox']").click(function(){
		var realestate_video_href = jQuery(this).attr('href'),
			realestate_video_link;

		realestate_vimeo = realestate_video_href.match(/vimeo.com\/(.*)/i);
		if ( realestate_vimeo != null ) realestate_video_link = 'http://player.vimeo.com/video/' + realestate_vimeo[1];
		else {
			realestate_youtube = realestate_video_href.match(/watch\?v=([^&]*)/i);
			if ( realestate_youtube != null ) realestate_video_link = 'http://youtube.com/embed/' + realestate_youtube[1];
		}

		jQuery.fancybox({
			'overlayOpacity' : 0.7,
			'overlayColor' : '#000000',
			'autoScale' : false,
			'transitionIn' : 'elastic',
			'transitionOut' : 'elastic',
			'easingIn' : 'easeOutBack',
			'easingOut' : 'easeInBack',
			'type' : 'iframe',
			'centerOnScroll' : true,
			'speedIn' : '700',
			'href' : realestate_video_link
		});
		return false;
	});

	var $portfolioItem = jQuery('.realestate_page_template_gallery_entry');
	$portfolioItem.find('.realestate_page_template_item_image').css('background-color','#000000');
	jQuery('.zoom-icon, .more-icon').css({'opacity':'0','visibility':'visible'});

	$portfolioItem.hover(function(){
		jQuery(this).find('.realestate_page_template_item_image').stop(true, true).animate({top: -10}, 500).find('img.portfolio').stop(true, true).animate({opacity: 0.7},500);
		jQuery(this).find('.zoom-icon').stop(true, true).animate({opacity: 1, left: 43},400);
		jQuery(this).find('.more-icon').stop(true, true).animate({opacity: 1, left: 110},400);
	}, function(){
		jQuery(this).find('.zoom-icon').stop(true, true).animate({opacity: 0, left: 31},400);
		jQuery(this).find('.more-icon').stop(true, true).animate({opacity: 0, left: 128},400);
		jQuery(this).find('.realestate_page_template_item_image').stop(true, true).animate({top: 0}, 500).find('img.portfolio').stop(true, true).animate({opacity: 1},500);
	});

	//contact page
	var $realestate_contact_container = jQuery('#realestate-contact'),
		$realestate_contact_form = $realestate_contact_container.find('form#realestate_contact_form'),
		$realestate_contact_submit = $realestate_contact_container.find('input#realestate_contact_submit'),
		$realestate_inputs = $realestate_contact_form.find('input[type=text],textarea'),
		realestate_email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,
		realestate_contact_error = false,
		$realestate_contact_message = jQuery('#realestate-contact-message'),
		realestate_message = '';

	$realestate_inputs.live('focus', function(){
		if ( jQuery(this).val() === jQuery(this).siblings('label').text() ) jQuery(this).val('');
	}).live('blur', function(){
		if (jQuery(this).val() === '') jQuery(this).val( jQuery(this).siblings('label').text());
	});

	$realestate_contact_form.live('submit', function() {
		realestate_contact_error = false;
		realestate_message = '<ul>';

		$realestate_inputs.removeClass('realestate_contact_error');

		$realestate_inputs.each(function(index, domEle){
			if ( jQuery(domEle).val() === '' || jQuery(domEle).val() === jQuery(this).siblings('label').text() ) {
				jQuery(domEle).addClass('realestate_contact_error');
				realestate_contact_error = true;

				var default_value = jQuery(this).siblings('label').text();
				if ( default_value === undefined ) default_value = 'Captcha';

				realestate_message += '<li>Fill ' + default_value + ' field</li>';
			}
			if ( (jQuery(domEle).attr('id') == 'realestate_contact_email') && !realestate_email_reg.test(jQuery(domEle).val()) ) {
				jQuery(domEle).removeClass('realestate_contact_error').addClass('realestate_contact_error');
				realestate_contact_error = true;

				if ( !realestate_email_reg.test(jQuery(domEle).val()) ) realestate_message += '<li>Invalid email</li>';
			}
		});

		if ( !realestate_contact_error ) {
			$href = jQuery(this).attr('action');

			$realestate_contact_container.fadeTo('fast',0.2).load($href+' #realestate-contact', jQuery(this).serializeArray(), function() {
				$realestate_contact_container.fadeTo('fast',1);
			});
		}

		realestate_message += '</ul>';

		if ( realestate_message != '<ul></ul>' )
			$realestate_contact_message.html(realestate_message);

		return false;
	});

	var $realestate_searchinput = jQuery('#realestate-searchinput');
		realestate_search_value = $realestate_searchinput.val();

	$realestate_searchinput.focus(function(){
		if (jQuery(this).val() === realestate_search_value) jQuery(this).val('');
	}).blur(function(){
		if (jQuery(this).val() === "") jQuery(this).val(realestate_search_value);
	});

	var $realestate_template_portfolio_thumb = jQuery('.realestate_page_template_portfolio_entry');
	$realestate_template_portfolio_thumb.hover(function(){
		jQuery(this).find('img').fadeTo('fast', 0.8);
		jQuery(this).find('.realestate_portfolio_more_icon,.realestate_portfolio_zoom_icon').fadeTo('fast', 1);
	}, function(){
		jQuery(this).find('img').fadeTo('fast', 1);
		jQuery(this).find('.realestate_portfolio_more_icon,.realestate_portfolio_zoom_icon').fadeTo('fast', 0);
	});

});